package com.ikangtai.shecare.record.bean;


import android.content.Context;

import com.ikangtai.shecare.common.App;
import com.ikangtai.shecare.common.db.DBManager;
import com.ikangtai.shecare.common.util.AppLog;
import com.ikangtai.shecare.common.util.DateUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

public class UserRecordData {

	private String id;
	private String userName;
	private long recordDate; //获取userRecordData实例的时间戳
	private long recordEditDate;
	private int symptomInfo;
	private int mucusInfo;
	private int mensesInfo;
	private String memoInfo;
	private long sexTime;
	private int copulationInfo;
	private  int ovulationInfo;
	private int isSynced;
  	private int LHResult;
	private int HCGResult;
	private int isOvulationDay;
	private int isDelete;
	public UserTemperatureInfo[] userTemperatureInfos;
	private long dayTime; //用于构造函数
	private Context context;
	public UserRecordData() {
	}

	public long getDayTime() {
		return dayTime;
	}

	public void setDayTime(long dayTime) {
		this.dayTime = dayTime;
	}

	public UserRecordData(Context context, long dayTime, String userName){
	  this.dayTime = dayTime;
	  this.userName = userName;
		DBManager dbManager = new DBManager(context);
		dbManager.setSelectedDayRecordDataInfo(this);
		List<UserTemperatureInfo> userTemperatureInfosList = dbManager.getSelectedDayTemperature(App.userName, DateUtil.getDateStr2bit(dayTime));
		if(userTemperatureInfosList.size() > 0){
			userTemperatureInfos = userTemperatureInfosList.toArray(new UserTemperatureInfo[userTemperatureInfosList.size()]);
			this.setUserTemperatureInfos(userTemperatureInfos);
		}
	}

	public static List<UserRecordData> getUserRecordDataList(Context context, String userName, long startTime, long endTime){
		DBManager dbManager = new DBManager(context);
		return dbManager.getRecordDataList(userName, startTime, endTime);
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public int getMensesInfo() {
		return mensesInfo;
	}

	public void setMensesInfo(int mensesInfo) {
		this.mensesInfo = mensesInfo;
	}

	public int getMucusInfo() {
		return mucusInfo;
	}

	public void setMucusInfo(int mucusInfo) {
		this.mucusInfo = mucusInfo;
	}

	public long getRecordDate() {
		return recordDate;
	}

	public void setRecordDate(long recordDate) {
		this.recordDate = recordDate;
	}

	public long getRecordEditDate() {
		return recordEditDate;
	}

	public void setRecordEditDate(long recordEditDate) {
		this.recordEditDate = recordEditDate;
	}

	public int getSymptomInfo() {
		return symptomInfo;
	}

	public void setSymptomInfo(int symptomInfo) {
		this.symptomInfo = symptomInfo;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public int getIsSynced() {
		return isSynced;
	}

	public void setIsSynced(int isSynced) {
		this.isSynced = isSynced;
	}

	public int getCopulationInfo() {
		return copulationInfo;
	}

	public void setCopulationInfo(int copulationInfo) {
		this.copulationInfo = copulationInfo;
	}

	public int getIsDelete() {
		return isDelete;
	}

	public void setIsDelete(int isDelete) {
		this.isDelete = isDelete;
	}

	public String getMemoInfo() {
		return memoInfo;
	}

	public void setMemoInfo(String memoInfo) {
		this.memoInfo = memoInfo;
	}

	public long getSexTime() {
		return sexTime;
	}

	public void setSexTime(long sexTime) {
		this.sexTime = sexTime;
	}

	public int getOvulationInfo() {
		return ovulationInfo;
	}

	public void setOvulationInfo(int ovulationInfo) {
		this.ovulationInfo = ovulationInfo;
	}

	public UserTemperatureInfo[] getUserTemperatureInfos() {
		return userTemperatureInfos;
	}

	public int getLHResult() {
		return LHResult;
	}

	public void setLHResult(int LHResult) {
		this.LHResult = LHResult;
	}

	public int getHCGResult() {
		return HCGResult;
	}

	public void setHCGResult(int HCGResult) {
		this.HCGResult = HCGResult;
	}

	public int getIsOvulationDay() {
		return isOvulationDay;
	}

	public void setIsOvulationDay(int isOvulationDay) {
		this.isOvulationDay = isOvulationDay;
	}

	public void setUserTemperatureInfos(UserTemperatureInfo[] userTemperatureInfos) {
		this.userTemperatureInfos = userTemperatureInfos;
	}

	public JSONObject getUserRecordDataInfoJson(){

		JSONObject jsonObject = new JSONObject();
		try {
			jsonObject.put("id", id);
			jsonObject.put("recordDate", DateUtil.getDateTimeStr2bit(recordDate));
			if(0 != recordEditDate) jsonObject.put("recordEditDate", DateUtil.getDateTimeStr2bit(recordEditDate));
			jsonObject.put("memo",memoInfo);
			jsonObject.put("mucilage", mucusInfo);
			jsonObject.put("menstruationProperty",mensesInfo);
			if(0 != sexTime) jsonObject.put("sexTime", DateUtil.getDateTimeStr2bit(sexTime));
			jsonObject.put("hadSex", copulationInfo);
			jsonObject.put("tag", symptomInfo);
			jsonObject.put("ovulatoryTest", ovulationInfo);
			if(null != userTemperatureInfos && userTemperatureInfos.length > 0)
			jsonObject.put("temperatures", getTemperJsonArray());

		} catch (JSONException e) {
			e.printStackTrace();
		}
		AppLog.i(jsonObject.toString());
		return jsonObject;
	}

	private JSONArray getTemperJsonArray() {

		JSONArray jsonTemperDatas = new JSONArray();
		JSONObject jsonObject;
		for(int i = 0; i < userTemperatureInfos.length; i++) {

			jsonObject = userTemperatureInfos[i].getUserTemperatureInfoJson();
			jsonTemperDatas.put(jsonObject);
		}
		return jsonTemperDatas;
	}

	public static boolean isMensesBlood(int mensesInfo){

		int mensesBlood = divideBytes(mensesInfo,2,0);
		return (mensesBlood > 0)?true : false;
	}

	public static int divideBytes(int value, int bits, int position){

		return (value>>position)&(int)(Math.pow(2, bits) - 1);
	}

	/**获得BBT */
	public double getBBT(){
		double bBT = 0;
		if(null != userTemperatureInfos && userTemperatureInfos.length > 0){
			for(int i = 0; i < userTemperatureInfos.length; i++){
				if(userTemperatureInfos[i].getIsBBT() == 1){
					bBT = userTemperatureInfos[i].getTemperature();
					break;
				}
			}
		}
		return bBT;
	}

    /**获得Mucus值 */
    public int getMucusStatusValue(){
        return divideBytes(mucusInfo, 3, 1);
    }

	/**是否测量时间过晚 */
	public boolean isMeasureLate(){
		return (divideBytes(symptomInfo, 1, 17) == 1) ? true:false;
	}

	/**是否熬夜(由之前失眠改为熬夜) */
	public boolean isLostSleep(){
		return (divideBytes(symptomInfo, 1, 5) == 1) ? true:false;
	}

	/**是否长途旅行 */
	public boolean isLongTrip(){
		return (divideBytes(symptomInfo, 1, 4) == 1) ? true:false;
	}

	/**是否倒时差 */
	public boolean isJetLag(){
		return (divideBytes(symptomInfo, 1, 18) == 1) ? true:false;
	}

	/**是否节假日生活失率 */
	public boolean isHolidayOutofLife(){
		return (divideBytes(symptomInfo, 1, 19) == 1) ? true:false;
	}

	/**是否气候变化 */
	public boolean isWeatherChange(){
		return (divideBytes(symptomInfo, 1, 20) == 1) ? true:false;
	}

	/**是否巨大压力 */
	public boolean isBigPressure(){
		return (divideBytes(symptomInfo, 1, 21) == 1) ? true:false;
	}

	/**是否异常兴奋 */
	public boolean isTooExcited(){
		return (divideBytes(symptomInfo, 1, 22) == 1) ? true:false;
	}

	/**是否过量饮酒 */
	public boolean isDrunk(){
		return (divideBytes(symptomInfo, 1, 3) == 1) ? true:false;
	}

	/**是否发烧 */
	public boolean isFever(){
		return (divideBytes(symptomInfo, 1, 7) == 1) ? true:false;
	}

	/**是否生病 */
	public boolean isSick(){
		return (divideBytes(symptomInfo, 1, 2) == 1) ? true:false;
	}

	/**是否非经期出血 */
	public boolean isNonMensesBlood(){
		return (divideBytes(symptomInfo, 1, 8) == 1) ? true:false;
	}

	/**是否眩晕 */
	public boolean isDizzy(){
		return (divideBytes(symptomInfo, 1, 9) == 1) ? true:false;
	}

	/**是否腰痛 */
	public boolean isBackAche(){
		return (divideBytes(symptomInfo, 1, 10) == 1) ? true:false;
	}

	/**是否烦躁 */
	public boolean isAnxious(){
		return (divideBytes(symptomInfo, 1, 11) == 1) ? true:false;
	}

	/**是否腹痛 */
	public boolean isBellyAche(){
		return (divideBytes(symptomInfo, 1, 12) == 1) ? true:false;
	}

	/**是否头痛 */
	public boolean isHeadache(){
		return (divideBytes(symptomInfo, 1, 13) == 1) ? true:false;
	}

	/**是否乳房胀痛 */
	public boolean isBreastAche(){
		return (divideBytes(symptomInfo, 1, 14) == 1) ? true:false;
	}

	/**是否身体酸痛 */
	public boolean isBodyAche(){
		return (divideBytes(symptomInfo, 1, 16) == 1) ? true:false;
	}

	/**是否同房 */
	public boolean isCopulationHaveSex(){
		return (divideBytes(copulationInfo, 1, 0) == 1) ? true:false;
	}
	/**是否月经出血 */
	public boolean isMensesBlood(){
		return (divideBytes(mensesInfo, 2, 0) == 1) ? true:false;
	}
	/**宫颈粘液状态 */
	public int getMucusStatus(){
		return divideBytes(mucusInfo, 3, 1);
	}

}
